package com.shenfa.util;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.OutputStream;
import java.net.MalformedURLException;
import java.net.URL;

import javax.servlet.http.HttpServletResponse;

import common.Logger;

public class ReadShowFile {
	private static Logger log=Logger.getLogger(ReadShowFile.class);
	/**
	 * 读取预览文件
	 * @param filepath 文件实体名
	 * @param response 返回对象
	 * @param onLine   是否在线读取true在线读取flash下载
	 */
	public static void read(String filepath,HttpServletResponse response,boolean onLine){
		log.debug("read:filepath="+filepath+",onLine="+onLine);
		if(null==filepath || "".equals(filepath)){
			return;
		}
		File f=new File(filepath);
		if(!f.exists()){
			log.error("read:file not exists");
			return;
		}
		try {
			BufferedInputStream br = new BufferedInputStream(new FileInputStream(f));   
	        byte[] buf = new byte[1024];   
	        int len = 0;   
	        response.reset();
	        if (onLine) {
	            URL u = new URL("file:///"+filepath);   
	            response.setContentType(u.openConnection().getContentType());   
	            response.setHeader("Content-Disposition", "inline; filename=" + f.getName());   
	               
	        } else {   
	            response.setContentType("application/x-msdownload");   
	            response.setHeader("Content-Disposition", "attachment; filename=" + f.getName());   
	        }
	        response.setCharacterEncoding("utf-8");
	        OutputStream out = response.getOutputStream();   
	        while ((len = br.read(buf)) > 0){   
	            out.write(buf, 0, len);   
	        }
	        out.close();
	        br.close();
	        
		} catch (FileNotFoundException e) {
			e.printStackTrace();
		} catch (MalformedURLException e) {
			e.printStackTrace();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	/**
	 * 创建一个xml
	 * @param newfilepath 创建的xml文件路径名称
	 * @param billtype   账单类型 1纸质2电子3短信
	 * @param area   模板区域
	 * @param pri	优先级
	 * @param furl  图片素材物理地址，文字素材的文字内容
	 * @param fhttp 素材的http地址
	 * @throws IOException 
	 */
	public static int createXml(String newfilepath,String xml) throws IOException{
		//获取运行类的物理路径
		// 获取服务器配置路径
		File oldfile=new File(Config.getValue("rule.preview.xml.path"));
		if(!oldfile.exists()){
			log.error("file not found." + oldfile.getAbsolutePath());
			throw new FileNotFoundException();
		}
		File newfile=new File(newfilepath);
		try {
			if(newfile.exists()){
				newfile.delete();
			}
			BufferedInputStream br = new BufferedInputStream(new FileInputStream(oldfile));
			BufferedOutputStream bw=new BufferedOutputStream(new FileOutputStream(newfile));
			byte[] buf = new byte[1024];
			int len = 0;
			while ((len = br.read(buf)) > 0){   
				bw.write(buf, 0, len);   
	        }
			bw.write(xml.getBytes("utf-8"));
			br.close();
			bw.close();

		} catch (IOException e1) {
			log.error("个性化区域预览xml文件操作错误");
			throw e1;
		}
		return 0;
	}
}
